package com.learn.service.impl;

import com.learn.service.FormService;
import com.learn.strategy.FormSubmitHandler;
import com.learn.strategy.FormSubmitHandlerFactory;
import com.learn.strategy.dto.CommonPairResponse;
import com.learn.strategy.dto.FormSubmitRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * @author tuzhijin
 * @version Id: FormServiceImpl.java, v 0.1 2021/3/21 下午2:06 tuzhijin Exp $$
 */
@Service
public class FormServiceImpl implements FormService {

    @Autowired
    private FormSubmitHandlerFactory submitHandlerFactory;

    @Override
    public CommonPairResponse submitForm(@NonNull FormSubmitRequest request) {
        String submitType = request.getSubmitType();

        // 根据 submitType 找到对应的提交处理器
        FormSubmitHandler submitHandler = submitHandlerFactory.getHandler(submitType);

        // 判断 submitType 对应的 handler 是否存在
        if (submitHandler == null) {
            return CommonPairResponse.failure("非法的提交类型: " + submitType);
        }

        // 处理提交
        return submitHandler.handleSubmit(request);
    }
}

